package com.nextive.weightlogger.util;

import java.util.regex.Pattern;

/**
 * String utilities.
 * 
 * @author Federico Baseggio <fedebaseggio@gmail.com>
 */
public class StringUtil {
	/**
	 * The pattern to validate e-mail addresses.
	 */
	private static final Pattern EMAIL_PATTERN = Pattern
			.compile("^[\\S^\\.]+@([\\S^\\.]+\\.)+[a-z]+$");
	
	/**
	 * Returns as string of the specified size consisting of the specified
	 * string and as many spaces as needed appended at the end.
	 * 
	 * @param string
	 *            The string to be right padded.
	 * @param size
	 *            The expected size of the string to be returned.
	 * @return The specified string right padded with spaces.
	 */
	public static String padRight(String string, int size) {
	     return String.format("%1$-" + size + "s", string);  
	}

	/**
	 * Returns as string of the specified size consisting of the specified
	 * string and as many spaces as needed appended at the beginning.
	 * 
	 * @param string
	 *            The string to be left padded.
	 * @param size
	 *            The expected size of the string to be returned.
	 * @return The specified string left padded with spaces.
	 */
	public static String padLeft(String string, int size) {
	    return String.format("%1$" + size + "s", string);  
	}

	/**
	 * Checks whether the specified email address is well formed.
	 * 
	 * @param emailAddress The email address to check.
	 * @return True if the email address is valid, false otherwise.
	 */
	public static boolean validateEmailAddress(String emailAddress)
	{
		return EMAIL_PATTERN.matcher(emailAddress).matches();
	}
}
